package com.zxy.ziems.server.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.btp.web.util.TokenUtils;
import com.zxy.ziems.server.domain.Role;
import com.zxy.ziems.server.dto.ChangePwdDto;
import com.zxy.ziems.server.dto.UserDto;
import com.zxy.ziems.server.dto.UserQueryDto;
import com.zxy.ziems.server.dto.UserRoleDto;
import com.zxy.ziems.server.service.UserService;
import com.zxy.ziems.server.vo.UserInfoVo;
import com.zxy.ziems.server.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 用户管理
 *
 * @author makejava
 * @since 2023-11-17 10:29:03
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 新增修改用户
     * @param userDto
     * @return
     */
    @PostMapping("/insertOrUpdate")
    public JsonResponse insertOrUpdate (@RequestBody @Validated UserDto userDto) {
        userService.insertOrUpdate(userDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 启停用户
     * @param userDto
     * @return
     */
    @PostMapping("/stopOrStart")
    public JsonResponse stopOrStart(@RequestBody UserDto userDto) {
        userService.stopOrStart(userDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 分配用户角色
     * @param userRoleDto
     * @return
     */
    @PostMapping("/divideRole")
    public JsonResponse<Role> divideRole(@RequestBody UserRoleDto userRoleDto) {
        userService.divideRole(userRoleDto);
        return (JsonResponse<Role>)JsonResponse.SUCCESS();
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @GetMapping("/resetPassword")
    public JsonResponse resetPassword(String userId) {
        userService.resetPassword(userId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse delete(String userId) {
        userService.removeById(userId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 修改密码
     * @param changePwdDto
     * @return
     */
    @PostMapping("/changePassword")
    public JsonResponse updatePwd(@RequestBody ChangePwdDto changePwdDto) {
        userService.changePassword(changePwdDto);
        return JsonResponse.SUCCESS("修改密码成功");
    }

    /**
     * 用户列表
     * @param userQueryDto
     * @return
     */
    @PostMapping("/pageList/old")
    public JsonResponse<PageResult<UserVo>> pageList (@RequestBody UserQueryDto userQueryDto) {
        PageResult<UserVo> pageResult = userService.pageList(userQueryDto);
        return JsonResponse.SUCCESS(pageResult);
    }

    /**
     * 用户列表(zifoms)
     * @param userQueryDto
     * @return
     */
    @PostMapping("/pageList")
    public JsonResponse<PageResult<UserVo>> pageListFoms (@RequestBody UserQueryDto userQueryDto) {
        PageResult<UserVo> pageResult = userService.pageListFoms(userQueryDto);
        return JsonResponse.SUCCESS(pageResult);
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    public JsonResponse<UserInfoVo> userInfo() {
        //获取登录人id
        String userId = TokenUtils.getUserIdByToken();
        return JsonResponse.SUCCESS(userService.getUserInfo(userId));
    }
}

